package com.fqxiny.aop01.configure;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : 戚欣扬
 * @Description :
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.data.redis.host}")
    private String host ;
    @Value("${spring.data.redis.password}")
    private String pasword;
    @Value("${spring.data.redis.port}")
    private String port;

    private String timeout;
    private static String ADDRESS_PREFIX = "redis://";
    @Bean
    RedissonClient redissonSingle(){
        Config config = new Config();

        if (host.isEmpty()){
            throw  new RuntimeException("Redisson Host is empty!");
        }
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(ADDRESS_PREFIX + host + ":" + port);

        if (timeout!=null) {
            serverConfig.setTimeout(Integer.parseInt(timeout));
        }
        if (!pasword.isEmpty()){
            serverConfig.setPassword(pasword);
        }
        return Redisson.create(config);
    }
}


